package com.sgb.kjwl.viewmodel.packers;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.self.ResponseList;
import com.sgb.kjwl.model.entity.self.SelfTeamDeviceEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.packers.WorkEquipmentActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @date 2021/3/15
 * @author F_Q
 * Description:
 */
public class WorkEquipmentModel extends BaseViewModel {
    WorkEquipmentActivity activity;
    HomeRepertory repertory;
    public WorkEquipmentModel(WorkEquipmentActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory=repertory;
    }

    public void refreshAndLoadMore(int page,String no) {
        Map<String,Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", AppConstant.PAGER_LIMIT);
        // 首选企业编号，如果企业编号为空，则传用户编号，两者不能同时为空，不能同时都有值
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        int userType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else if (userType == 2){
            map.put("teamRoleNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }else {
            map.put("companyNo", companyNo);
        }
        //列表默认传0，工队与业绩关联接口页面传参为 1
        map.put("teamByType", 1);
        //工队与业绩关联接口页面传参为 当前工队的编号
        map.put("teamNo", "");
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamDeviceList(map).enqueue(new ICallBack<BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>> data) {
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.setWorkEquipmentData(data.getData().getList());
                } else {
                    if (page <= 1) {
                        activity.showEmpty();
                    }
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (page <= 1) {
                    activity.showEmpty();
                }
            }
        });
    }

    public void setAddRelation(List<String> noLists, String no, String type) {
        Map<String,Object> map = new HashMap<>();
        map.put("relation", noLists);
        map.put("teamNo", no);
        map.put("type", type);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().setAddRelation(map).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                //成功后的处理
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)){
                    TeamParticulars(no);
                }
            }
        });
    }

    /**
     * 跳转到h5详情
     * @param no
     */
    public void TeamParticulars(String no) {
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    //参数1:type(0:	前台,1:用户中心);	参数2:no
                    String url = data.getData().getUrl() + "?no=" +no + "&type=1"+"&index=3";
                  activity.setUrlView(url);
                }
            }
        });
    }

    /**
     * 设备详情
     * @param no
     */
    public void getH5Url(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("groupEquipmentDeatil").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no + "&isdele=1";
                    WebViewX5HostActivity.start(activity, url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
